import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.font.TextAttribute;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import twitter4j.Trend;
import twitter4j.TwitterException;
import twitter4j.internal.org.json.JSONException;

public class LocationTrendPanel extends Panel {

	private static final long serialVersionUID = -3844300447912194733L;

	public LocationTrendPanel() {
		super();

		JPanel main = new JPanel();
		main.setLayout(new BorderLayout());
		main.setPreferredSize(new Dimension(852, 480));

		JLabel label = new JLabel("Location: ");
		JTextField input = new JTextField("Chicago");
		JButton update = new JButton("Update Trends");

		JPanel inputPanel = new JPanel();

		input.setColumns(20);

		inputPanel.add(label);
		inputPanel.add(input);

		main.add(inputPanel, BorderLayout.NORTH);

		JPanel tweetParent = new JPanel();
		tweetParent.setLayout(new BorderLayout());
		JPanel tweetPanel = new JPanel();
		GridLayout g = new GridLayout(11, 1, 10, 20);
		tweetPanel.setBorder(BorderFactory.createEmptyBorder(5, 30, 5, 5));

		tweetPanel.setLayout(g);
		tweetPanel.setMaximumSize(new Dimension(140, 480));
		JLabel tweets = new JLabel("Tweets");
		tweets.setBorder(BorderFactory.createEmptyBorder(5, 90, 5, 5));
		tweetParent.add(tweets, BorderLayout.NORTH);

		JPanel urlParent = new JPanel();
		urlParent.setLayout(new BorderLayout());

		JPanel urlPanel = new JPanel();
		urlPanel.setLayout(g);
		urlPanel.setPreferredSize(new Dimension(440, 480));

		JLabel links = new JLabel("Links");
		links.setBorder(BorderFactory.createEmptyBorder(5, 90, 5, 5));

		urlParent.add(links, BorderLayout.NORTH);

		// TODO Auto-generated method stub
		TwitterLocationTrend t = new TwitterLocationTrend();

		Trend[] trends = null;
		try {
			trends = t.getTrends("Chicago");
		} catch (TwitterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (Trend trend : trends) {
			JTextField trendText = new JTextField(trend.getName());
			trendText.setColumns(20);
			tweetPanel.add(trendText);

			JLabel urlText = new JLabel(trend.getURL());
			urlText.addMouseListener(new openUrl(urlText));
			urlText.setForeground(new Color(0x3366BB));
			urlPanel.add(urlText);

		}

		tweetParent.add(tweetPanel);
		urlParent.add(urlPanel);
		main.add(tweetParent, BorderLayout.WEST);
		main.add(urlParent, BorderLayout.EAST);
		LocationTrendListener trendListener = new LocationTrendListener(input,
				tweetPanel, urlPanel, main, this);
		update.addActionListener(trendListener);
		inputPanel.add(update);
		this.add(main);
	}

	public class openUrl implements MouseListener {

		boolean pressed = false;
		JLabel label;

		public openUrl(JLabel label) {
			this.label = label;
		}

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			Font font = label.getFont();
			Map attributes = font.getAttributes();
			attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
			label.setFont(font.deriveFont(attributes));
			if (!(pressed))
				label.setForeground(new Color(0x0B0080));
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			Font font = label.getFont();
			Map attributes = font.getAttributes();
			attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE);
			label.setFont(font.deriveFont(attributes));
			if (!(pressed))
				label.setForeground(new Color(0x3366BB));
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			try {
				Desktop.getDesktop().browse(new URI(label.getText()));
				label.setForeground(new Color(0x663366));
				pressed = true;
			} catch (IOException e) {
				label.setText("Invalid Link");
			} catch (URISyntaxException e) {
				label.setText("Invalid Link");
			}
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

	}

	public class LocationTrendListener implements ActionListener {

		String location;
		JPanel tweetPanel, urlPanel, main;
		LocationTrendPanel trendPanel;
		JTextField input;

		public LocationTrendListener(JTextField input, JPanel left,
				JPanel right, JPanel main, LocationTrendPanel trendPanel) {

			this.input = input;
			this.tweetPanel = left;
			this.urlPanel = right;
			this.main = main;
			this.trendPanel = trendPanel;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			trendPanel.remove(main);
			JPanel tempTweetPanel = (JPanel) tweetPanel.getParent();
			JPanel urlTweetPanel = (JPanel) urlPanel.getParent();
			main.remove(tempTweetPanel);
			main.remove(urlTweetPanel);
			tempTweetPanel.remove(tweetPanel);
			urlTweetPanel.remove(urlPanel);
			TwitterLocationTrend t = new TwitterLocationTrend();
			location = input.getText();
			Trend[] trends = null;
			try {
				trends = t.getTrends(location);
			} catch (TwitterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tweetPanel.removeAll();
			urlPanel.removeAll();
			for (Trend trend : trends) {

				JTextField trendText = new JTextField(trend.getName());
				trendText.setColumns(20);
				tweetPanel.add(trendText);
				System.out.println(trend.getName());
				JLabel urlText = new JLabel(trend.getURL());
				urlText.addMouseListener(new openUrl(urlText));
				urlText.setForeground(new Color(0x3366BB));
				urlPanel.add(urlText);

			}
			tempTweetPanel.add(tweetPanel);
			urlTweetPanel.add(urlPanel);
			main.add(tempTweetPanel, BorderLayout.WEST);
			main.add(urlTweetPanel, BorderLayout.EAST);
			trendPanel.add(main);
			trendPanel.revalidate();
			trendPanel.repaint();

		}
	}
}
